package com.shop.cereshop.app.service.map.impl;

import com.shop.cereshop.app.param.map.OffsetBoundsRequest;
import com.shop.cereshop.app.service.map.OffsetBoundsService;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.map.OffsetBoundsResp;
import com.shop.cereshop.commons.utils.GeoUtils;
import org.springframework.stereotype.Service;

@Service
public class OffsetBoundsServiceImpl implements OffsetBoundsService {

    @Override
    public OffsetBoundsResp generateBounds(CereBuyerUser user, OffsetBoundsRequest param) {
        Double lng = param.getLng();
        Double lat = param.getLat();

        // 使用 GeoUtils 根据中心点和长宽(左右各100米=宽200， 上下各50米=高100) 计算四个顶点
        double[][] vertices = GeoUtils.rectangleVerticesByCenter(lng, lat, 200.0, 100.0);

        String coordString = String.format("%.6f,%.6f;%.6f,%.6f;%.6f,%.6f;%.6f,%.6f",
                vertices[0][0], vertices[0][1],
                vertices[1][0], vertices[1][1],
                vertices[2][0], vertices[2][1],
                vertices[3][0], vertices[3][1]);

        OffsetBoundsResp resp = new OffsetBoundsResp();
        resp.setPointName(param.getPointName());
        resp.setPointDesc(param.getPointDesc());
        resp.setCenterLng(lng);
        resp.setCenterLat(lat);
        resp.setVectorImage();
        // 顶点辅助字段
        resp.setWestLng(vertices[0][0]);
        resp.setNorthLat(vertices[0][1]);
        resp.setEastLng(vertices[1][0]);
        resp.setSouthLat(vertices[2][1]);
        resp.setCoordString(coordString);
        return resp;
    }
}


